package cn.changein.framework.service.setting.thirdInterfaceConfig.impl;

import cn.changein.framework.base.pojo.page.PageFactory;
import cn.changein.framework.base.pojo.page.PageInfo;
import cn.changein.framework.model.setting.thirdInterfaceConfig.entity.ThirdInterfaceConfig;
import cn.changein.framework.model.setting.thirdInterfaceConfig.mapper.ThirdInterfaceConfigMapper;
import cn.changein.framework.model.setting.thirdInterfaceConfig.model.params.ThirdInterfaceConfigParam;
import cn.changein.framework.model.setting.thirdInterfaceConfig.model.result.ThirdInterfaceConfigResult;
import cn.changein.framework.service.setting.thirdInterfaceConfig.ThirdInterfaceConfigService;
import cn.changein.framework.util.ToolUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.util.List;


/**
 * <p>
 * 对接第三方接口的配置 服务实现类
 * </p>
 *

 * @Date 2021-01-21
 * @Copyright: companyName
 * @Version 1.0
 * @Since JDK1.8
 */
@Service
public class ThirdInterfaceConfigServiceImpl extends ServiceImpl<ThirdInterfaceConfigMapper, ThirdInterfaceConfig> implements ThirdInterfaceConfigService {

    @Override
    public void add(ThirdInterfaceConfigParam param) {
        ThirdInterfaceConfig entity = getEntity(param);
        this.save(entity);
    }

    @Override
    public void delete(ThirdInterfaceConfigParam param) {
        this.removeById(getKey(param));
    }

    @Override
    public void update(ThirdInterfaceConfigParam param) {
        ThirdInterfaceConfig oldEntity = getOldEntity(param);
        ThirdInterfaceConfig newEntity = getEntity(param);
        ToolUtil.copyProperties(newEntity, oldEntity);
        this.updateById(newEntity);
    }

    @Override
    public ThirdInterfaceConfigResult findBySpec(ThirdInterfaceConfigParam param) {
        return null;
    }

    @Override
    public List<ThirdInterfaceConfigResult> findListBySpec(ThirdInterfaceConfigParam param) {
        return null;
    }

    @Override
    public PageInfo findPageBySpec(ThirdInterfaceConfigParam param) {
        Page pageContext = getPageContext();
        IPage page = this.baseMapper.customPageList(pageContext, param);
        return PageFactory.createPageInfo(page);
    }

    @Override
    public ThirdInterfaceConfig selectByCode(String code) {
        QueryWrapper<ThirdInterfaceConfig> queryWrapper = new QueryWrapper<ThirdInterfaceConfig>();
        queryWrapper.eq("code",code);
        queryWrapper.eq("status",1);
        return this.baseMapper.selectOne(queryWrapper);
    }

    private Serializable getKey(ThirdInterfaceConfigParam param) {
        return param.getId();
    }

    private Page getPageContext() {
        return PageFactory.defaultPage();
    }

    private ThirdInterfaceConfig getOldEntity(ThirdInterfaceConfigParam param) {
        return this.getById(getKey(param));
    }

    private ThirdInterfaceConfig getEntity(ThirdInterfaceConfigParam param) {
        ThirdInterfaceConfig entity = new ThirdInterfaceConfig();
        ToolUtil.copyProperties(param, entity);
        return entity;
    }

}
